<template>
  <div class="wrapper" ref="wrapper">
    <div class="center">
      <slot></slot>
    </div>
  </div>
</template>

<script>
  import BScroll from 'better-scroll'

  export default {
    name: "Scroll",
    props: {
      probeType: {
        type: Number,
        default: 0
      },
      pullUpLoad: {
        type: Boolean,
        default: false
      },
    },
    data() {
      return {
        BScroll: null,
      }
    },
    methods: {
      backTopClick(x, y, time = 500) {
        this.BScroll && this.BScroll.scrollTo(x, y, time);
      },
      finishPullUp() {
        setTimeout(() => {
          this.BScroll && this.BScroll.finishPullUp()
        }, 2000)
      },
      refresh() {
        this.BScroll && this.BScroll.refresh()
      },
      scrollToY(saveY) {
        this.BScroll.scrollTo(0, saveY, 0);
      },
      getScrollY() {
        return this.BScroll ? this.BScroll.y : 0;
      },
      scrollToElement(el, time = 500, offsetX, offsetY, easing) {
        this.BScroll.scrollToElement(el, time, offsetX, offsetX, easing)
      }
    },
    mounted() {
      this.BScroll = new BScroll(this.$refs.wrapper, {
        click: true,
        probeType: this.probeType,
        pullUpLoad: this.pullUpLoad,
        observeDOM: true
      })

      //滚动监听
      this.BScroll.on("scroll", position => {
        this.$emit("scroll", position);
      })

      //上拉加载更多
      if (this.pullUpLoad) {
        this.BScroll.on("pullingUp", () => {
          this.$emit("pullingUp");
          this.refresh();
        })
      }
    }
  }
</script>

<style scoped>
  .wrapper {
    overflow: hidden;
  }
</style>
